<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>moment.js</title>

    <script src="../../../../libs/moment.js/2.20.1/moment.min.js"></script>
    <script src="../../../../libs/moment.js/2.20.1/locale/zh-cn.js"></script>

</head>

<body>

    <script>
        //node设置语言
        //var moment = require('moment');
        //moment.locale('zh-cn');
        //一、显示
        //年
        var time = moment().format('YY'); console.log(time) //70 71 ... 29 30
        var time = moment().format('YYYY'); console.log(time) //1970 1971 ... 2029 2030
        //月
        var time = moment().format('M'); console.log(time) //9
        var time = moment().format('Mo'); console.log(time) //9th
        var time = moment().format('MM'); console.log(time) //09
        var time = moment().format('MMM'); console.log(time) //Sep
        var time = moment().format('MMMM'); console.log(time) //September
        //日
        var time = moment().format('D'); console.log(time) //8
        var time = moment().format('Do'); console.log(time) //18th
        var time = moment().format('DD'); console.log(time) //08
        //小时
        var time = moment().format('H'); console.log(time) //0 1 ... 22 23
        var time = moment().format('HH'); console.log(time) //00 01 ... 22 23
        var time = moment().format('h'); console.log(time) //1 2 ... 11 12
        var time = moment().format('hh'); console.log(time) //01 02 ... 11 12
        //分
        var time = moment().format('m'); console.log(time) //0 1 ... 58 59
        var time = moment().format('mm'); console.log(time) //00 01 ... 58 59
        //秒
        var time = moment().format('s'); console.log(time) //0 1 ... 58 59
        var time = moment().format('ss'); console.log(time) //00 01 ... 58 59
        //毫秒
        var time = moment().format('S'); console.log(time) //0 1 ... 8 9
        var time = moment().format('SS'); console.log(time) //00 01 ... 98 99
        var time = moment().format('SSS'); console.log(time) //000 001 ... 998 999
        var time = moment().format('SSSS'); console.log(time) //000[0..] 001[0..] ... 998[0..] 999[0..]
        //星期几
        var time = moment().format('d'); console.log(time) // 0 1 ... 5 6
        var time = moment().format('do'); console.log(time) // 0th 1st ... 5th 6th
        var time = moment().format('dd'); console.log(time) // Su Mo ... Fr Sa
        var time = moment().format('ddd'); console.log(time) // Sun Mon ... Fri Sat
        var time = moment().format('e'); console.log(time) //0 1 ... 5 6
        var time = moment().format('E'); console.log(time) //1

        //时刻
        var time = moment().format('Q'); console.log(time) //3
        //一年内第几天
        var time = moment().format('DDD'); console.log(time) // 1 2 ... 364 365
        var time = moment().format('DDDo'); console.log(time) //1st 2nd ... 364th 365th 
        var time = moment().format('DDDD'); console.log(time) //001 002 ... 364 365

        //一年内第几周
        var time = moment().format('w'); console.log(time) //1 2 ... 52 53
        var time = moment().format('wo'); console.log(time) //1st 2nd ... 52nd 53rd
        var time = moment().format('ww'); console.log(time) //01 02 ... 52 53
        //显示AM/PM
        var time = moment().format('A'); console.log(time) //AM PM
        var time = moment().format('a'); console.log(time) //am pm
        //时间戳
        var time = moment().format('X'); console.log(time) //秒 1360013296
        var time = moment().format('x'); console.log(time) //毫秒 1360013296123 

        //本地化格式
        var time = moment().format('LT'); console.log(time) //      8:30 PM
        var time = moment().format('LTS'); console.log(time) //     8:30:25 PM
        var time = moment().format('L'); console.log(time) //       09/04/1986
        var time = moment().format('l'); console.log(time) //       9/4/1986
        var time = moment().format('LL'); console.log(time) //      September 4 1986
        var time = moment().format('ll'); console.log(time) //      Sep 4 1986
        var time = moment().format('LLL'); console.log(time) //     September 4 1986 8:30 PM
        var time = moment().format('lll'); console.log(time) //     Sep 4 1986 8:30 PM
        var time = moment().format('LLLL'); console.log(time) //    Thursday, September 4 1986 8:30 PM
        var time = moment().format('llll'); console.log(time) //    Thu, Sep 4 1986 8:30 PM
        //转义字符  要在格式字符串中转义字符，可以将字符包装在方括号中。
        var time = moment().format('[今天是] dddd'); console.log(time)// '今天是 Monday'
        //二、时差 以现在为基准
        //时差以现在为基准
        //moment().fromNow();
        //moment().fromNow(Boolean);
        var time = moment([2007, 0, 29]).fromNow(); console.log(time) // 有后缀4 years ago
        var time = moment([2007, 0, 29]).fromNow(true); console.log(time) //没有后缀 4 years

        //时差 (之前)
        //moment().from(Moment|String|Number|Date|Array);
        //moment().from(Moment|String|Number|Date|Array, Boolean);  
        var a = moment([2007, 0, 28]);
        var b = moment([2007, 0, 29]);
        var time = a.from(b); console.log(time)// "a day ago"

        var a = moment([2007, 0, 28]);
        var b = moment([2007, 0, 29]);
        a.from(b);                     // "a day ago"
        a.from([2007, 0, 29]);         // "a day ago"
        a.from(new Date(2007, 0, 29)); // "a day ago"
        a.from("2007-01-29");          // "a day ago"

        var start = moment([2007, 0, 5]);
        var end = moment([2007, 0, 10]);
        end.from(start);       // "in 5 days"
        end.from(start, true); // "5 days"
        //时差（之后）以现在为基准
        //moment().toNow();
        //moment().toNow(Boolean);
        moment([2007, 0, 29]).toNow(); // in 4 years
        moment([2007, 0, 29]).toNow(true); // 4 years
        //时差（之后）
        //moment().to(Moment|String|Number|Date|Array);
        //moment().to(Moment|String|Number|Date|Array, Boolean);
        var a = moment([2007, 0, 28]);
        var b = moment([2007, 0, 29]);
        a.to(b) // "in a day"

        var a = moment([2007, 0, 28]);
        var b = moment([2007, 0, 29]);
        a.to(b);                     // "in a day"
        a.to([2007, 0, 29]);         // "in a day"
        a.to(new Date(2007, 0, 29)); // "in a day"
        a.to("2007-01-29");          // "in a day"

        var start = moment([2007, 0, 5]);
        var end = moment([2007, 0, 10]);
        end.to(start);       // "5 days ago"
        end.to(start, true); // "5 days"
        //三、数组
        console.log(moment("2017-01-01").toArray()) // [2017, 0, 1, 0, 0, 0, 0]
        //四、对象
        console.log(moment().toObject())    //{years: 2017, months: 8, date: 18, hours: 15, minutes: 47, …}
        //五、是否之前
        //moment().isBefore(Moment|String|Number|Date|Array);
        //moment().isBefore(Moment|String|Number|Date|Array, String);
        moment('2010-10-20').isBefore('2010-10-21'); // true
        moment('2010-10-20').isBefore('2010-12-31', 'year'); // false
        moment('2010-10-20').isBefore('2011-01-01', 'year'); // true
        //六、是否相同
        //moment().isSame(Moment|String|Number|Date|Array);
        //moment().isSame(Moment|String|Number|Date|Array, String); 
        moment('2010-10-20').isSame('2010-10-20'); // true
        moment('2010-10-20').isSame('2009-12-31', 'year');  // false
        moment('2010-10-20').isSame('2010-01-01', 'year');  // true
        moment('2010-10-20').isSame('2010-12-31', 'year');  // true
        moment('2010-10-20').isSame('2011-01-01', 'year');  // false
        moment('2010-01-01').isSame('2011-01-01', 'month'); // false, different year
        moment('2010-01-01').isSame('2010-02-01', 'day');   // false, different month
        //七、是否之后
        //moment().isAfter(Moment|String|Number|Date|Array);
        //moment().isAfter(Moment|String|Number|Date|Array, String);
        moment('2010-10-20').isAfter('2010-10-19'); // true
        moment('2010-10-20').isAfter('2010-01-01', 'year'); // false
        moment('2010-10-20').isAfter('2009-12-31', 'year'); // true
        //八、是否之间 
        //moment().isBetween(moment-like, moment-like);
        //moment().isBetween(moment-like, moment-like, String); 
        moment('2010-10-20').isBetween('2010-10-19', '2010-10-25'); // true 
        moment('2010-10-20').isBetween('2010-01-01', '2012-01-01', 'year'); // false
        moment('2010-10-20').isBetween('2009-12-31', '2012-01-01', 'year'); // true
        //自定义
        //月份名
        moment.locale('en', {
            months: [
                "January", "February", "March", "April", "May", "June", "July",
                "August", "September", "October", "November", "December"
            ]
        });
        //月份缩写
        moment.locale('en', {
            monthsShort: [
                "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
            ]
        });
        //星期名
        moment.locale('en', {
            weekdays: [
                "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
            ]
        });
        //星期缩写
        moment.locale('en', {
            weekdaysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
        });
        //相对时间
        moment.locale('en', {
            relativeTime: {
                future: "in %s",
                past: "%s ago",
                s: "seconds",
                m: "a minute",
                mm: "%d minutes",
                h: "an hour",
                hh: "%d hours",
                d: "a day",
                dd: "%d days",
                M: "a month",
                MM: "%d months",
                y: "a year",
                yy: "%d years"
            }
        });
        //长格式
        moment.locale('en', {
            longDateFormat: {
                LT: "h:mm A",
                LTS: "h:mm:ss A",
                L: "MM/DD/YYYY",
                l: "M/D/YYYY",
                LL: "MMMM Do YYYY",
                ll: "MMM D YYYY",
                LLL: "MMMM Do YYYY LT",
                lll: "MMM D YYYY LT",
                LLLL: "dddd, MMMM Do YYYY LT",
                llll: "ddd, MMM D YYYY LT"
            }
        });
        //上午、下午
        moment.locale('zh-cn', {
            meridiem: function (hour, minute, isLowercase) {
                if (hour < 9) {
                    return "早上";
                } else if (hour < 11 && minute < 30) {
                    return "上午";
                } else if (hour < 13 && minute < 30) {
                    return "中午";
                } else if (hour < 18) {
                    return "下午";
                } else {
                    return "晚上";
                }
            }
        });

    </script>
</body>

</html>